import { createReducer } from 'reduxsauce'
import { REHYDRATE } from 'redux-persist/es/constants'
import {
  GET_TAG_FEED_LIST,
  GET_TAG_DETAIL,
  GET_TAG_SURVEYS,
  GET_TAG_DOCTORS,
  GET_TAG_VIDEOS,
  GET_TAG_FEATURED,
  GET_TAG_PLAN,
  GET_TAG_GOODS
} from '@ACTIONS/tag'

const INITIAL_STATE = {
  tagDetail: {},
  loadingTagDetail: true,
  loadError: false,
  loadingTagSurveys: true,
  tagSurveys: [],
  loadingTagDoctors: true,
  tagDoctorMore: false,
  tagDoctors: [],
  loadingTagFeatureds: true,
  tagFeatureds: [],
  loadingTagVideos: true,
  tagVideos: [],
  loadingTagFeedList: false,
  tagFeedList: [],
  canLoadMoreTagFeedList: true,
  videoListTotal: 0,
  plans: [],
  tagGoods: [],
}

const rehydrate = (state, { payload }) => {
  if (payload && payload.tag) {
    return { ...INITIAL_STATE, ...payload.tag }
  }
  return { ...state }
}

export default createReducer(INITIAL_STATE, {
  [REHYDRATE]: rehydrate,

  [GET_TAG_DETAIL.REQUEST]: state => ({ ...state, loadError: false, loadingTagDetail: true }),
  [GET_TAG_DETAIL.SUCCESS]: (state, { data }) => ({
    ...state, tagDetail: data, loadError: false, loadingTagDetail: false
  }),
  [GET_TAG_DETAIL.FAILURE]: state => ({ ...state, loadError: true, loadingTagDetail: false }),

  [GET_TAG_FEED_LIST.REQUEST]: (state, { payload: { skip } = {} }) => ({
    ...state,
    loadingTagFeedList: true,
    tagFeedList: skip === 0 ? [] : state.templateAndPacks,
  }),
  [GET_TAG_FEED_LIST.SUCCESS]: (state, { data, canLoadMore }) => ({
    ...state,
    tagFeedList: data,
    loadingTagFeedList: false,
    canLoadMoreTemplateAndPacks: canLoadMore,
  }),
  [GET_TAG_SURVEYS.REQUEST]: state => ({
    ...state,
    loadingTagSurveys: true,
    tagSurveys: [],
  }),
  [GET_TAG_SURVEYS.SUCCESS]: (state, { data }) => ({
    ...state,
    tagSurveys: data,
    loadingTagSurveys: false
  }),
  [GET_TAG_SURVEYS.REQUEST]: state => ({
    ...state,
    loadingTagSurveys: true,
    tagSurveys: [],
  }),
  [GET_TAG_FEATURED.SUCCESS]: (state, { data }) => ({
    ...state,
    tagFeatureds: data,
    loadingTagFeatureds: false
  }),
  [GET_TAG_FEATURED.REQUEST]: state => ({
    ...state,
    loadingTagFeatureds: true,
    tagFeatureds: [],
  }),
  [GET_TAG_DOCTORS.SUCCESS]: (state, { data, tagDoctorMore }) => ({
    ...state,
    tagDoctors: data,
    tagDoctorMore,
    loadingTagDoctors: false
  }),
  [GET_TAG_DOCTORS.REQUEST]: state => ({
    ...state,
    loadingTagDoctors: true,
    tagDoctorMore: false,
    tagDoctors: [],
  }),
  [GET_TAG_DOCTORS.FAILURE]: state => ({
    ...state,
    loadingTagDoctors: false
  }),
  [GET_TAG_VIDEOS.SUCCESS]: (state, { data }) => ({
    ...state,
    tagVideos: data,
    loadingTagVideos: false
  }),
  [GET_TAG_VIDEOS.REQUEST]: state => ({
    ...state,
    loadingTagVideos: true,
    tagVideos: [],
  }),
  [GET_TAG_PLAN.SUCCESS]: (state, { data }) => ({
    ...state,
    plans: data
  }),
  [GET_TAG_PLAN.REQUEST]: state => ({
    ...state,
    plans: [],
  }),
  [GET_TAG_FEED_LIST.REQUEST]: (state, skip) => {
    if (skip === 0) {
      return ({
        ...state,
        loadingTagFeedList: true,
        tagFeedList: [],
      })
    }
    return state
  },
  [GET_TAG_FEED_LIST.SUCCESS]: (state, { data, canLoadMore }) => ({
    ...state,
    tagFeedList: data,
    loadingTagFeedList: false,
    canLoadMoreTagFeedList: canLoadMore
  }),
  [GET_TAG_FEED_LIST.FAILURE]: state => ({ ...state, loadingTagFeedList: false }),
  [GET_TAG_GOODS.SUCCESS]: (state, { data }) => ({
    ...state,
    tagGoods: data,
  }),
  [GET_TAG_GOODS.REQUEST]: state => ({
    ...state,
    tagGoods: [],
  }),
})
